using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Search.PageAdapters.Utilities;

namespace Search.PageAdapters
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("d8094425-177f-4430-a6d7-f964f9a2e654")]
    public class BasicSearchPageFixupForV4EventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = null;
            // The following if/else allows the feature scope to be changed without any significant refactoring required.
            if (properties.Feature.Parent is SPSite)
            {
                SPSite _site = properties.Feature.Parent as SPSite;
                if (_site != null)
                {
                    web = _site.RootWeb;
                }
            }
            else if (properties.Feature.Parent is SPWeb)
            {
                web = properties.Feature.Parent as SPWeb;
            }

            try
            {
                if (web != null)
                {
                    if (web.WebTemplate == "SRCHCENTERLITE")
                    {
                        SearchAdapters.ApplyV4SearchFixToBasicSearchCenter(web , true);
                    }
                    else
                    {
                        // Not a basic search center
                        throw new InvalidOperationException("The feature cannot be activated because the current site is not a Basic Search Center.");
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("There was an error while activating the feature {0}", properties.Feature.ToString()), e);
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = null;
            // The following if/else allows the feature scope to be changed without any significant refactoring required.
            if (properties.Feature.Parent is SPSite)
            {
                SPSite _site = properties.Feature.Parent as SPSite;
                if (_site != null)
                {
                    web = _site.RootWeb;
                }
            }
            else if (properties.Feature.Parent is SPWeb)
            {
                web = properties.Feature.Parent as SPWeb;
            }

            try
            {
                if (web != null)
                {
                    if (web.WebTemplate == "SRCHCENTERLITE")
                    {
                        SearchAdapters.ApplyV4SearchFixToBasicSearchCenter(web, false);
                    }
                    else
                    {
                        // Not a basic search center
                        throw new InvalidOperationException("The feature cannot be activated because the current site is not a Basic Search Center.");
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("There was an error while deactivating the feature {0}", properties.Feature.ToString()), e);
            }
        }
    }
}
